<template>
	<view class="flexbe top">
		<view class="flexst topl" :class="[collapse ? 'logobox' : 'logobox2']">
			<image src="../../static/logo.png" class="logo"></image>
			<text class="cor-ff b" v-if="collapse">PC后台管理系统</text>
		</view>
		<view class="flexbe topr" :class="[collapse ? 'topr-width' : 'topr-width2']">
			<view @click="setLeftWindow" class="collapse-btn">
				<el-icon v-if="collapse"><Fold /></el-icon>
				<el-icon v-else><Expand /></el-icon>
			</view>
			<view class="flexed info">
				<el-dropdown>
					<view class="flexst ml15">
						<uni-icons type="person" size="16" color="#000000"></uni-icons>
						<view class="ml5">{{ userInfo?.username }}</view>
					</view>
					<template #dropdown>
						<el-dropdown-menu>
							<el-dropdown-item @click="dialogFormVisible = true">修改密码</el-dropdown-item>
							<el-dropdown-item @click="centerDialogVisible = true">退出</el-dropdown-item>
						</el-dropdown-menu>
					</template>
				</el-dropdown>
			</view>
		</view>

		<!-- <el-icon><Expand /></el-icon> -->
	</view>
	<!-- 对话框 -->
	<view>
		<el-dialog v-model="dialogFormVisible" title="修改密码">
			<el-form ref="formRef" :model="dynamicValidateForm" label-width="120px" class="demo-dynamic">
				<el-form-item
					prop="oldPssword"
					label="原密码"
					:rules="[
						{
							required: true,
							message: '请输入原密码',
							trigger: 'blur'
						}
					]"
				>
					<el-input v-model="dynamicValidateForm.oldPssword" />
				</el-form-item>
				<el-form-item
					prop="password"
					label="新密码"
					:rules="[
						{
							required: true,
							message: '请输入新密码',
							trigger: 'blur'
						}
					]"
				>
					<el-input v-model="dynamicValidateForm.password" />
				</el-form-item>
				<el-form-item
					prop="newPassword"
					label="确认新密码"
					:rules="[
						{
							required: true,
							message: '请确认新密码',
							trigger: 'blur'
						}
					]"
				>
					<el-input v-model="dynamicValidateForm.newPassword" />
				</el-form-item>
				<el-form-item>
					<el-button @click="dialogFormVisible = false">取消</el-button>
					<el-button type="primary" @click="submitForm(formRef)">确定</el-button>
				</el-form-item>
			</el-form>
		</el-dialog>
	</view>

	<el-dialog v-model="centerDialogVisible" title="提示" width="30%">
		<span>确定退出登录吗</span>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="centerDialogVisible = false">取消</el-button>
				<el-button type="primary" @click="loginOut">确定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import {
	reactive,
	ref,
	toRefs
} from 'vue';
import router from '@/router';
import type {
	FormInstance,
} from 'element-plus'
import {
	Expand,
	Fold
} from '@element-plus/icons-vue'
const collapse = ref(true)
const centerDialogVisible = ref(false)
const data = reactive({
	userInfo: uni.getStorageSync('userInfo') || {},
});
const {
	userInfo
} = toRefs(data);

//监听登录后设置用户名
uni.$on('setUsername', info => {
	data.userInfo = info;
});

// 左侧菜单栏收缩
const setLeftWindow = () => {
	uni.$emit('setLeftWindow')
	collapse.value = !collapse.value
}

const loginOut = () => {
	centerDialogVisible.value = false
	uni.clearStorageSync();
	uni.reLaunch({
		url: '/pages/login/login'
	});
};
const dialogFormVisible = ref(false)
const formRef = ref < FormInstance > ()
const dynamicValidateForm = reactive({
	oldPassword: '',
	password: '',
	newPassword: ''
})

const submitForm = (formEl: FormInstance | undefined) => {
	if (!formEl) return
	formEl.validate((valid) => {
		if (valid) {
			console.log('submit!')
		} else {
			console.log('error submit!')
			return false
		}
	})
}
</script>

<style lang="scss">
.top {
	.topl {
		padding-left: 15px;
		background: #29374b;
		height: 60px;
	}

	.topr-width {
		width: calc(100% - 240px);
	}

	.topr-width2 {
		width: calc(100% - 64px);
	}

	.topr {
		background: #ffffff;
		height: 60px;
		padding-right: 20px;
		box-sizing: border-box;
		border-bottom: 1px solid #eeeeee;

		.tabBtn {
			padding: 0 10px;
			font-size: 12px;
			margin: 0 5px;
			line-height: 24px;
			border: 1px solid #abafb6;
			border-radius: 2px;
			cursor: pointer;

			.mr5 {
				margin-right: 5px;
			}
		}

		.active {
			background: #409eff;
			color: #ffffff;
			border: none;
		}

		.c9 {
			cursor: pointer;
		}

		.ml15 {
			margin-left: 15px;
		}

		.ml5 {
			margin-left: 5px;
		}

		.fs14 {
			font-size: 14px;
		}

		.pl20 {
			padding-left: 20px;
		}
	}
}

.logobox2 {
	width: 64px;
	font-size: 20px;
	padding-left: 20px;
	box-sizing: border-box;

	.logo {
		width: 30px;
		height: 30px;
		margin-right: 10px;
	}
}

.logobox {
	width: 240px;
	font-size: 20px;
	padding-left: 20px;
	box-sizing: border-box;

	.logo {
		width: 30px;
		height: 30px;
		margin-right: 10px;
	}
}

.menu {
	width: 20px;
	height: 20px;
	margin-right: 5px;
}

.el-tag {
	margin: 0 5px;
}

.collapse-btn {
	padding: 0 20px;
	height: 60px;
	line-height: 60px;
	cursor: pointer;
}

.el-icon {
	font-size: 20px;
}
.info {
	cursor: pointer;
}
</style>
